// import Cesium from 'cesium'

import * as Cesium from 'cesium/Source/Cesium'  //正确
import "cesium/Build/Cesium/Widgets/widgets.css";
import Log from "../../plugins/utils/log";
import Environment from "../../plugins/utils/environment";
window.Cesium = require("cesium");

window.Cesium.Ion.defaultAccessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI0NmYwZmRkZi0xNWIzLTQ1NzUtYjUzNi0zMjE2YWUzM2Y5NWUiLCJpZCI6NzIwMjAsImlhdCI6MTYzNTc0NDgxNX0.I06SsT5T9yc551brR_ceOrP_k7RXE2-KRxoZtf237TQ";
export default class Map{
    static __instance = null;
    static instance = function(){
        if(Map.__instance == null){
            Map.__instance = new Map();
        }
        return Map.__instance;
    }

    static init(){
        let imageProvider = null;
        let terrainProvider = null;
        // if (Setting.DataSource == Setting.DataSourceType.HTTP_DATA) {
        //     imageProvider = new Cesium.UrlTemplateImageryProvider({
        //         url: Setting.HTTP_IMAGE_LAYER_URL
        //     });
        //     if (Setting.terrainEnabled == true) {
        //         if (Setting.HTTP_TERRAIN_LAYER_URL.length > 0) {
        //             terrainProvider = new Cesium.CesiumTerrainProvider({
        //                 url: Setting.HTTP_TERRAIN_LAYER_URL
        //             });
        //         } else {
        //             terrainProvider = Cesium.createWorldTerrain();
        //         }
        //     }
        // } else {
        //     imageProvider = new Cesium.UrlTemplateImageryProvider({
        //         url: this.imgPrefix + Environment.getDataBasePath() + "/Image/" + "{z}/{x}/{y}"
        //     });
        //     terrainProvider = new Cesium.CesiumTerrainProvider({
        //         url: this.demPrefix + Environment.getDataBasePath() + "/Dem/"
        //     });
        // }
        var subdomains=['0','1','2','3','4','5','6','7'];
        imageProvider=new Cesium.WebMapTileServiceImageryProvider({
            url: "http://t{s}.tianditu.com/img_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=img&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=28890177b959659a402f18fe3af41dc1",
            layer: "tdtBasicLayer",
            subdomains: subdomains,
            style: "default",
            format: "image/jpeg",
            tileMatrixSetID: "GoogleMapsCompatible"
        });
        // terrainProvider =  new Cesium.CesiumTerrainProvider({
        //     url: Cesium.IonResource.fromAssetId(1),
        // });
        //Cesium.IonResource.fromAssetId(1);
        // imageProvider = new Cesium.ArcGisMapServerImageryProvider({
        //     url: "http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer"
        // });
        //terrainProvider = Cesium.createWorldTerrain();
        let viewer = new Cesium.Viewer("cesiumContainer", {
            animation: true,
            baseLayerPicker: false,
            fullscreenButton: false,
            geocoder: false,
            timeline: true,
            shouldAnimate: false,
            sceneModePicker: false,
            navigationHelpButton: false,
            infoBox: false,
            selectionIndicator: false,
            homeButton: false,
            skyBox: false,
            contextOptions: {
                webgl:{
                    alpha: true,
                    depth:true,
                    stencil:true,
                    antialias:true,
                    premultipliedAlpha:true,
                    //通过canvas.toDataURL()实现截图需要将该项设置为true
                    preserveDrawingBuffer:true,
                    failIfMajorPerformanceCaveat:true
                }
            },
            imageryProvider: imageProvider,
            terrainProvider: terrainProvider
        });
        viewer.scene.debugShowFramesPerSecond = true;
        // viewer.scene.debugShowFramesPerSecond = false;

        //开启深度测试
        viewer.scene.globe.depthTestAgainstTerrain = true;
        //自动匹配字体分辨率
        viewer.scene.fxaa = true;
        viewer.scene.postProcessStages.fxaa.enabled = true;
        viewer.scene.logarithmicDepthBuffer  = false;

        if (Cesium.FeatureDetection.supportsImageRenderingPixelated()) {
            viewer.resolutionScale = window.devicePixelRatio;
        }

        viewer.cesiumWidget.creditContainer.style.display = "none";
        viewer.animation.container.style.display = "none";
        viewer.timeline.container.style.display = "none";


        // var yellowEdge = Cesium.PostProcessStageLibrary.createDepthViewStage();
        // // yellowEdge.uniforms.color = Cesium.Color.YELLOW;
        //
        // viewer.scene.postProcessStages.add(yellowEdge);
        // viewer.scene.debugShowFrustums = true;
        // viewer.scene.postRender.addEventListener(function (c,b) {
        //     let pp = c.postProcessStages[0];
        // })

        window.cesiumViewer = viewer;
        return viewer;
    }
}
